/*
 * $Id: shannon.c 20 2005-05-24 02:39:42Z philwil $
 *
 * Copyright (C) 2002, 2003 ETC s.r.o.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * Written by Richard Klingler <richard@uclinux.net>, 2004.
 *
 * Documentation:
 * [1] Intel Corporation, "Intel StrongARM SA-1100 Microprocessor
 *
 */

#include "sysdep.h"

#include <stdlib.h>
#include <stdint.h>
#include <string.h>

#include "part.h"
#include "bus.h"
#include "chain.h"
#include "bssignal.h"
#include "jtag.h"
#include "buses.h"

typedef struct {
	chain_t *chain;
	part_t *part;
	signal_t *a[26];
	signal_t *d[32];
	signal_t *ncs[4];
	signal_t *nwe;
	signal_t *noe;
	signal_t *ncas[4];
	signal_t *nras[4];
	signal_t *npce[2];
	signal_t *npoe;
	signal_t *npwe;
	signal_t *npior;
	signal_t *npiow;
} bus_params_t;

#define	CHAIN	((bus_params_t *) bus->params)->chain
#define	PART	((bus_params_t *) bus->params)->part
#define	A		((bus_params_t *) bus->params)->a
#define	D		((bus_params_t *) bus->params)->d
#define	nCS		((bus_params_t *) bus->params)->ncs
#define	nWE		((bus_params_t *) bus->params)->nwe
#define	nOE		((bus_params_t *) bus->params)->noe
#define	nCAS	((bus_params_t *) bus->params)->ncas
#define	nRAS	((bus_params_t *) bus->params)->nras
#define	nPCE	((bus_params_t *) bus->params)->npce
#define	nPWE	((bus_params_t *) bus->params)->npwe
#define	nPOE	((bus_params_t *) bus->params)->npoe
#define	nPIOR	((bus_params_t *) bus->params)->npior
#define	nPIOW	((bus_params_t *) bus->params)->npiow

static void
setup_address( bus_t *bus, uint32_t a )
{
	int i;
	part_t *p = PART;

	for (i = 0; i < 26; i++)
		part_set_signal( p, A[i], 1, (a >> i) & 1 );
}

static int shannon_bus_area( bus_t *bus, uint32_t adr, bus_area_t *area );

static void
set_data_in( bus_t *bus )
{
	int i;
	part_t *p = PART;
	bus_area_t area;

	shannon_bus_area( bus, 0, &area );

	for (i = 0; i < area.width; i++)
		part_set_signal( p, D[i], 0, 0 );
}

static void
setup_data( bus_t *bus, uint32_t d )
{
	int i;
	part_t *p = PART;
	bus_area_t area;

	shannon_bus_area( bus, 0, &area );

	for (i = 0; i < area.width; i++)
		part_set_signal( p, D[i], 1, (d >> i) & 1 );
}

static void
shannon_bus_printinfo( bus_t *bus )
{
	int i;

	for (i = 0; i < CHAIN->parts->len; i++)
		if (PART == CHAIN->parts->parts[i])
			break;
	printf( _("Intel SA-1100 compatible bus driver for Tuxscreen (JTAG part No. %d)\n"), i );
}

static void
shannon_bus_prepare( bus_t *bus )
{       
	part_set_instruction( PART, "EXTEST" );
	chain_shift_instructions( CHAIN );
}

static void
shannon_bus_read_start( bus_t *bus, uint32_t adr )
{
	/* see Figure 10-12 in [1] */
	part_t *p = PART;
	chain_t *chain = CHAIN;

	part_set_signal( p, nCS[0], 1, 0 );
	part_set_signal( p, nCS[1], 1, 1 );
	part_set_signal( p, nCS[2], 1, 1 );
	part_set_signal( p, nCS[3], 1, 1 );
	part_set_signal( p, nWE, 1, 1 );
	part_set_signal( p, nOE, 1, 0 );

	part_set_signal( p, nCAS[0], 1, 1 );
	part_set_signal( p, nCAS[1], 1, 1 );
	part_set_signal( p, nCAS[2], 1, 1 );
	part_set_signal( p, nCAS[3], 1, 1 );
	part_set_signal( p, nRAS[0], 1, 1 );
	part_set_signal( p, nRAS[1], 1, 1 );
	part_set_signal( p, nRAS[2], 1, 1 );
	part_set_signal( p, nRAS[3], 1, 1 );

	part_set_signal( p, nPCE[0], 1, 1 );
	part_set_signal( p, nPCE[1], 1, 1 );
	part_set_signal( p, nPOE, 1, 1 );
	part_set_signal( p, nPWE, 1, 1 );
	part_set_signal( p, nPIOR, 1, 1 );
	part_set_signal( p, nPIOW, 1, 1 );

	setup_address( bus, adr );
	set_data_in( bus );

	chain_shift_data_registers( chain, 0 );
}

static uint32_t
shannon_bus_read_next( bus_t *bus, uint32_t adr )
{
	/* see Figure 10-12 in [1] */
	part_t *p = PART;
	chain_t *chain = CHAIN;
	int i;
	uint32_t d = 0;
	bus_area_t area;

	shannon_bus_area( bus, adr, &area );

	setup_address( bus, adr );
	chain_shift_data_registers( chain, 1 );

	for (i = 0; i < area.width; i++)
		d |= (uint32_t) (part_get_signal( p, D[i] ) << i);

	return d;
}

static uint32_t
shannon_bus_read_end( bus_t *bus )
{
	/* see Figure 10-12 in [1] */
	part_t *p = PART;
	chain_t *chain = CHAIN;
	int i;
	uint32_t d = 0;
	bus_area_t area;

	shannon_bus_area( bus, 0, &area );

	part_set_signal( p, nCS[0], 1, 1 );
	part_set_signal( p, nCS[1], 1, 1 );
	part_set_signal( p, nCS[2], 1, 1 );
	part_set_signal( p, nCS[3], 1, 1 );
	part_set_signal( p, nOE, 1, 1 );
	part_set_signal( p, nCAS[0], 1, 1 );
	part_set_signal( p, nCAS[1], 1, 1 );
	part_set_signal( p, nCAS[2], 1, 1 );
	part_set_signal( p, nCAS[3], 1, 1 );
	part_set_signal( p, nRAS[0], 1, 1 );
	part_set_signal( p, nRAS[1], 1, 1 );
	part_set_signal( p, nRAS[2], 1, 1 );
	part_set_signal( p, nRAS[3], 1, 1 );

	part_set_signal( p, nPCE[0], 1, 1 );
	part_set_signal( p, nPCE[1], 1, 1 );
	part_set_signal( p, nPOE, 1, 1 );
	part_set_signal( p, nPWE, 1, 1 );
	part_set_signal( p, nPIOR, 1, 1 );
	part_set_signal( p, nPIOW, 1, 1 );

	chain_shift_data_registers( chain, 1 );

	for (i = 0; i < area.width; i++)
		d |= (uint32_t) (part_get_signal( p, D[i] ) << i);

	return d;
}

static uint32_t
shannon_bus_read( bus_t *bus, uint32_t adr )
{
	shannon_bus_read_start( bus, adr );
	return shannon_bus_read_end( bus );
}

static void
shannon_bus_write( bus_t *bus, uint32_t adr, uint32_t data )
{
	/* see Figure 10-16 in [1] */
	part_t *p = PART;
	chain_t *chain = CHAIN;
	
	part_set_signal( p, nCS[0], 1, 0 );
	part_set_signal( p, nCS[1], 1, 1 );
	part_set_signal( p, nCS[2], 1, 1 );
	part_set_signal( p, nCS[3], 1, 1 );
	part_set_signal( p, nCAS[0], 1, 1 );
	part_set_signal( p, nCAS[1], 1, 1 );
	part_set_signal( p, nCAS[2], 1, 1 );
	part_set_signal( p, nCAS[3], 1, 1 );
	part_set_signal( p, nRAS[0], 1, 1 );
	part_set_signal( p, nRAS[1], 1, 1 );
	part_set_signal( p, nRAS[2], 1, 1 );
	part_set_signal( p, nRAS[3], 1, 1 );
	part_set_signal( p, nWE, 1, 1 );
	part_set_signal( p, nOE, 1, 1 );

	part_set_signal( p, nPCE[0], 1, 1 );
	part_set_signal( p, nPCE[1], 1, 1 );
	part_set_signal( p, nPOE, 1, 1 );
	part_set_signal( p, nPWE, 1, 1 );
	part_set_signal( p, nPIOR, 1, 1 );
	part_set_signal( p, nPIOW, 1, 1 );

	setup_address( bus, adr );
	setup_data( bus, data );

	chain_shift_data_registers( chain, 0 );

	part_set_signal( p, nWE, 1, 0 );
	chain_shift_data_registers( chain, 0 );
	part_set_signal( p, nWE, 1, 1 );
	part_set_signal( p, nCS[0], 1, 1 );
	part_set_signal( p, nCS[1], 1, 1 );
	part_set_signal( p, nCS[2], 1, 1 );
	part_set_signal( p, nCS[3], 1, 1 );
	part_set_signal( p, nCAS[0], 1, 1 );
	part_set_signal( p, nCAS[1], 1, 1 );
	part_set_signal( p, nCAS[2], 1, 1 );
	part_set_signal( p, nCAS[3], 1, 1 );
	part_set_signal( p, nRAS[0], 1, 1 );
	part_set_signal( p, nRAS[1], 1, 1 );
	part_set_signal( p, nRAS[2], 1, 1 );
	part_set_signal( p, nRAS[3], 1, 1 );

	part_set_signal( p, nPCE[0], 1, 1 );
	part_set_signal( p, nPCE[1], 1, 1 );
	part_set_signal( p, nPOE, 1, 1 );
	part_set_signal( p, nPWE, 1, 1 );
	part_set_signal( p, nPIOR, 1, 1 );
	part_set_signal( p, nPIOW, 1, 1 );

	chain_shift_data_registers( chain, 0 );
}

static int
shannon_bus_area( bus_t *bus, uint32_t adr, bus_area_t *area )
{
	area->description = NULL;
	area->start = 0x00000000;
//PSW	area->length = 0x100000000;
	area->length = 0x10000000;
	area->width = 32;

	return 0;
}

static void
shannon_bus_free( bus_t *bus )
{
	free( bus->params );
	free( bus );
}

static bus_t *shannon_bus_new( void );

const bus_driver_t shannon_bus = {
	"shannon",
	N_("Intel SA-1100 compatible bus driver for Tuxscreen"),
	shannon_bus_new,
	shannon_bus_free,
	shannon_bus_printinfo,
	shannon_bus_prepare,
	shannon_bus_area,
	shannon_bus_read_start,
	shannon_bus_read_next,
	shannon_bus_read_end,
	shannon_bus_read,
	shannon_bus_write
};

static bus_t *
shannon_bus_new( void )
{
	bus_t *bus;
	char buff[10];
	int i;
	int failed = 0;

	if (!chain || !chain->parts || chain->parts->len <= chain->active_part || chain->active_part < 0)
		return NULL;

	bus = malloc( sizeof (bus_t) );
	if (!bus)
		return NULL;

	bus->driver = &shannon_bus;
	bus->params = malloc( sizeof (bus_params_t) );
	if (!bus->params) {
		free( bus );
		return NULL;
	}

	CHAIN = chain;
	PART = chain->parts->parts[chain->active_part];

	for (i = 0; i < 26; i++) {
		sprintf( buff, "A(%d)", i );
		A[i] = part_find_signal( PART, buff );
		if (!A[i]) {
			printf( _("signal '%s' not found\n"), buff );
			failed = 1;
			break;
		}
	}
	for (i = 0; i < 32; i++) {
		sprintf( buff, "D(%d)", i );
		D[i] = part_find_signal( PART, buff );
		if (!D[i]) {
			printf( _("signal '%s' not found\n"), buff );
			failed = 1;
			break;
		}
	}
	for (i = 0; i < 4; i++) {
		sprintf( buff, "CS(%d)", i );
		nCS[i] = part_find_signal( PART, buff );
		if (!nCS[i]) {
			printf( _("signal '%s' not found\n"), buff );
			failed = 1;
			break;
		}
	}

	for (i = 0; i < 4; i++) {
		sprintf( buff, "RAS(%d)", i );
		nRAS[i] = part_find_signal( PART, buff );
		if (!nRAS[i]) {
			printf( _("signal '%s' not found\n"), buff );
			failed = 1;
			break;
		}
	}

	for (i = 0; i < 4; i++) {
		sprintf( buff, "CAS(%d)", i );
		nCAS[i] = part_find_signal( PART, buff );
		if (!nCAS[i]) {
			printf( _("signal '%s' not found\n"), buff );
			failed = 1;
			break;
		}
	}

	nWE = part_find_signal( PART, "WE" );
	if (!nWE) {
		printf( _("signal '%s' not found\n"), "nWE" );
		failed = 1;
	}

	nOE = part_find_signal( PART, "OE" );
	if (!nOE) {
		printf( _("signal '%s' not found\n"), "nOE" );
		failed = 1;
	}

	for (i = 0; i < 2; i++) {
		sprintf( buff, "PCE%d", i + 1);
		nPCE[i] = part_find_signal( PART, buff );
		if (!nPCE[i]) {
			printf( _("signal '%s' not found\n"), buff );
			failed = 1;
			break;
		}
	}

	nPOE = part_find_signal( PART, "POE" );
	if (!nPOE) {
		printf( _("signal '%s' not found\n"), "nPOE" );
		failed = 1;
	}

	nPWE = part_find_signal( PART, "PWE" );
	if (!nPWE) {
		printf( _("signal '%s' not found\n"), "nPWE" );
		failed = 1;
	}

	nPIOR = part_find_signal( PART, "PIOR" );
	if (!nPIOR) {
		printf( _("signal '%s' not found\n"), "nPIOR" );
		failed = 1;
	}

	nPIOW = part_find_signal( PART, "PIOW" );
	if (!nPIOW) {
		printf( _("signal '%s' not found\n"), "nPIOW" );
		failed = 1;
	}

	if (failed) {
		free( bus->params );
		free( bus );
		return NULL;
	}

	return bus;
}

